package com.web.service;


import com.web.VO.MyIdelVO;
import com.web.VO.MyPublishVO;
import com.web.VO.MySupplyVO;
import com.web.dao.NoticeMapper;
import com.web.dao.SupplyInfoMapper;
import com.web.dao.SupplyLightenMapper;
import com.web.entity.*;
import com.web.enums.NoticeEnum;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class SupplyService {
    @Resource
    private SupplyInfoMapper supplyMapper;
    @Resource
    private SupplyLightenMapper supplyLightenMapper;
    @Resource
    private NoticeMapper noticeMapper;

    /**
     * 用户发布供应
     * @param supply
     */
    public void insertSelective(Supply supply) {
        supplyMapper.insertSelective(supply);
    }


    /**
     * 供应列表
     * @return
     */
    public List<SupplyList> getSupplyList() {
       return  supplyMapper.getSupplyList();
    }

    /**
     * 四闲基础
     * @return
     */
    public List<LeaveUnused> LeaveuNusedList() {
        return supplyMapper.LeaveuNusedList();
    }

    /**
     * 供应详情
     * @param id
     * @param userId
     * @return
     */
    public SupplyDetails getSupplyDetails(Integer id, String userId) {
        return supplyMapper.getSupplyDetails(id,userId);
    }

    /**
     * 用户发布供应列表
     * @param user_id
     * @return
     */
    public List<SupplyList> findUserSupplyList(String user_id) {
        return supplyMapper.findUserSupplyList(user_id);
    }


    /**
     * 发布四闲
     * @param leaveUnusedInfo
     */
    public void insertReleaseFourIdles(LeaveUnusedInfo leaveUnusedInfo) {
        supplyMapper.insertReleaseFourIdles(leaveUnusedInfo);
    }

    /**
     * 四闲列表
     * @param
     * @return
     */
    public List<FourIdleLists> getFourIdleLists(Integer lu_id) {

        return supplyMapper.getFourIdleLists(lu_id);
    }

    /**
     * 获取四闲详情
     * @param id
     * @param user_id
     * @return
     */
    public FourIdleDetails getFourIdleDetails(int id, String user_id) {
        FourIdleDetails life = supplyMapper.getFourIdleDetails(id,user_id);
        if (life != null){
            life.setSee_num(life.getSee_num() + 1);
            life.setModify_time(new Date());
            supplyMapper.updateFourIdleDetails(life);
        }
        return life;
    }

    /**
     * 查询四闲
     * @param typeId
     * @return
     */
    public FourIdleDetails findUserFourIdle(Integer typeId) {
        return supplyMapper.findUserFourIdle(typeId);
    }

    /**
     * 修改收藏量
     * @param userDetails
     */
    public void updateCollectionNum(FourIdleDetails userDetails) {
        supplyMapper.updateCollectionNum(userDetails);
    }

    /**
     * 我发布的供应
     * @param userId
     * @return
     */
    public List<MyPublishVO> listMyPublishSupply(String userId) {
        return supplyMapper.listMyPublishSupply(userId);
    }

    /**
     * 我发布的四闲
     * @param userId
     * @return
     */
    public List<MyPublishVO> listMyPublishIdel(String userId) {
        return supplyMapper.listMyPublishIdel(userId);
    }

    /**
     * 条件查询点亮信息
     * @param supplyLighten
     * @return
     */
    public SupplyLighten getSupplyLightenInfo(SupplyLighten supplyLighten) {
        return supplyLightenMapper.getSupplyLightenInfo(supplyLighten);
    }

    /**
     * 通过用户id和供应id查询供应点亮信息
     * @param userId
     * @param supplyId
     * @return
     */
    public Integer countSupplyLighten(String userId, Integer supplyId) {
        return supplyLightenMapper.countSupplyLighten(userId, supplyId);
    }

    /**
     * 帮他点亮
     * @param supplyLighten
     */
    public void insertSupplyLightenInfo(SupplyLighten supplyLighten) {
        supplyLightenMapper.insertSelective(supplyLighten);

        // 点亮发送通知
        try {
            Notice notice = new Notice();
            notice.setSender(supplyLighten.getUserId());
            notice.setTarget(supplyLighten.getBombaId());
            notice.setType(NoticeEnum.LIGHTEN_NOTICE.getCode());
            notice.setTitle("供应点亮");
            SupplyDetails supplyDetail = supplyMapper.getSupplyDetails(supplyLighten.getSupplyId(), null);
            notice.setContent(supplyDetail.getSupply_desc());
            notice.setContentId(supplyLighten.getSupplyId());
            noticeMapper.insertSelective(notice);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 闲置详情用户主页历史发布
     * @param user_id
     * @return
     */
    public List<FourIdleLists> getUserIdleDetails(String user_id) {
        return supplyMapper.getUserIdleDetails(user_id);
    }


    /**
     * 查询我发布的供应
     * @param userId
     * @param typeId
     * @return
     */
    public Supply findMySupply(String userId, Integer typeId) {
        return supplyMapper.findMySupply(userId,typeId);
    }
    /**
     * 删除我发布的供应
     * @param typeId
     * @param typeId
     * @return
     */
    public void DeleteMySupply(Integer typeId) {
        supplyMapper.DeleteMySupply(typeId);
    }

    /**
     * 删除我的闲置
     * @param typeId
     */
    public void DeleteLeaveUnusedInfo(Integer typeId) {
        supplyMapper.DeleteLeaveUnusedInfo(typeId);
    }

    /**
     * 查询我的闲置
     * @param userId
     * @param typeId
     * @return
     */
    public LeaveUnusedInfo findMyLeaveUnusedInfo(String userId, Integer typeId) {
        return supplyMapper.findMyLeaveUnusedInfo(userId,typeId);
    }
}
